from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
iris = load_iris()
X = iris.data[:, 2:] # petal length and width
y = iris.target
# 创建决策树
tree_clf = DecisionTreeClassifier(max_depth=2, random_state=42)
# 训练模型
tree_clf.fit(X,y)

# 可视化输出决策树
from sklearn.tree import export_graphviz
export_graphviz(
        tree_clf,
        out_file="D:/logs/iris_tree.dot",
        feature_names=iris.feature_names[2:],
        class_names=iris.target_names,
        rounded=True,
        filled=True
    )

